import * as Cesium from 'cesium';

class CSYSConvert {
  constructor() {

  }
  // 局部坐标转成世界坐标
  localToWorld (localPos, modelMatrix) {
    // 1. 获取模型的顶点坐标
    const vertices = localPos;//geometry.attributes.position.values;  // 获取顶点坐标
    // 2. 获取模型的 modelMatrix（假设已知）
    // const modelMatrix = geometryInstance.modelMatrix;  // 或者某个模型的变换矩阵
    // 3. 将局部坐标转换为世界坐标
    const transformedVertices = [];
    for (let i = 0; i < vertices.length; i += 3) {
      const localPosition = new Cesium.Cartesian3(vertices[i], vertices[i + 1], vertices[i + 2]);
      const worldPosition = Cesium.Matrix4.multiplyByPoint(modelMatrix, localPosition, new Cesium.Cartesian3());
      transformedVertices.push(worldPosition);
    }
    return transformedVertices;
  }
  // 世界坐标转成局部坐标
  worldToLocal (worldPos, modelMatrix) {
    // 1. 获取模型的顶点坐标
    const vertices = worldPos;//geometry.attributes.position.values;  // 获取顶点坐标
    // 2. 获取模型的 modelMatrix（假设已知）
    // const modelMatrix = geometryInstance.modelMatrix;  // 或者某个模型的变换矩阵
    // 3. 将世界坐标转换为局部坐标
    const transformedVertices = [];
    for (let i = 0; i < vertices.length; i += 3) {
      const worldPosition = new Cesium.Cartesian3(vertices[i], vertices[i + 1], vertices[i + 2]);
      const localPosition = Cesium.Matrix4.multiplyByPoint(Cesium.Matrix4.inverse(modelMatrix, new Cesium.Matrix4()), worldPosition, new Cesium.Cartesian3());
      transformedVertices.push(localPosition);
    }
    return transformedVertices;
  }
  // 屏幕坐标转成世界坐标
  screenToWorld (screenPos, viewer) {
    const ray = viewer.camera.getPickRay(screenPos);
    const worldPos = viewer.scene.globe.pick(ray, viewer.scene);
    return worldPos;
  }
}

export default CSYSConvert;
